package com.example.nowcodercommunity.entity;

/**
 * 封装分页信息类
 */
public class Page {
    private int currentPage = 1;
    // 当前页码

    private int limit = 10;
    // 显示上限，默认为10

    private int totalRows;
    // 数据总数

    private String searchPath;
    // 查询路径

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if(currentPage>=1){
            this.currentPage = currentPage;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if(limit>=1&&limit<=100) {
            this.limit = limit;
        }
    }

    public int getTotalRows() {
        return totalRows;
    }

    public void setTotalRows(int totalRows) {
        if(totalRows>0) {
            this.totalRows = totalRows;
        }
    }

    public String getSearchPath() {
        return searchPath;
    }


    public void setSearchPath(String searchPath) {
        this.searchPath = searchPath;
    }

    /**
     * 获取当前页起始行数
     * @return
     */
    public int getFrom(){
        return (currentPage - 1) * limit;
    }

    /**
     * 通过总查询行数以及限制获取总页数
     * @return
     */
    public int getTotalPages(){
        if(limit==0){
            throw new RuntimeException("page limit cannot be zero!");
        }
        if(totalRows % limit == 0){
            return totalRows / limit;
        }else{
            return totalRows / limit + 1;
        }
    }

    /**
     * 设置显示于页面中的页码起始页
     * @return
     */
    public int getStartIndex(){
        return (currentPage <= 3)?1:currentPage-2;
    }

    /**
     * 设置显示于页面中的页码结束页
     * @return
     */
    public int getEndIndex(){
        int totalPage = getTotalPages();
        return (currentPage <= totalPage-2)?currentPage+2:totalPage;
    }
}
